package homelesspartners.client.widgets;

import java.util.ArrayList;

import homelesspartners.client.ServerFacade;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ThankyouTabWidget extends Composite {

	private VerticalPanel panel = new VerticalPanel();
	private HTML thankyouMessage =
			new HTML(
					"<b>Thank You for Your Help!</b><br /><br />"
							+ "Thanks to kind and generous people like you, we've collected caring gifts and messages for <b>'many'</b> homeless people already this Christmas!  Thank you so very much for your help in making this a happy time for many who would otherwise have nothing.<br /><br />Please click on <b>'responses'</b> above to read some of the expressions of gratitude from the people who received gifts through the program this Christmas.<br /><br /><br />"
							+ "<b>More Gifts Still Needed</b><br /><br />"
							+ "Below is a list of the number of people who have not yet received a gift in each city.  Our goal is to try and provide at least 1 gift to each person at the shelters.  Please tell your friends, family, and co-workers about this program to help make this goal a reality!<br /><br />");

	private Widget content = thankyouMessage;
	private static ThankyouTabWidget instance = new ThankyouTabWidget();

	public static ThankyouTabWidget getInstance() {
		return instance;
	}

	public ThankyouTabWidget() {

		//TODO this should happen when you click on the thank you tab (only once)
		ServerFacade.getThankYouStats();
		
		MenuBar menu = new MenuBar();
		menu.addItem(new MenuItem("thanks", new Command() {

			public void execute() {
				setContent(thankyouMessage);
			}
		}));
		menu.addItem(new MenuItem("responses", new Command() {

			public void execute() {
				setContent(ThankYouNotesWidget.getInstance());
				ThankYouNotesWidget.getInstance().refresh();
			}
		}));
		panel.add(menu);

		Image image = new Image("./images/fader.png");
		image.setWidth("728px");
		panel.add(image);

		panel.add(thankyouMessage);
		initWidget(panel);
	}

	public void initThankYouMessage(ArrayList stats) {
		String thankyouText = thankyouMessage.getHTML();
		System.out.println(thankyouText);
		String replaceText = "'many'";
		int giftedIndex = thankyouText.indexOf(replaceText);
		System.out.println(giftedIndex);
		String substring1 = thankyouText.substring(0, giftedIndex);
		String substring2 = thankyouText.substring(giftedIndex + replaceText.length());
		
		String newText = substring1;
		newText += stats.get(0).toString();
		newText += substring2;
		newText += stats.get(1).toString();
		newText += "<br /><br />";
		
		thankyouMessage.setHTML(newText);
	}

	void setContent(Widget widget) {
		panel.remove(content);
		content = widget;
		panel.add(content);
	}
}
